import Dialog from '../../vant-weapp/dialog/dialog';
import Toast from '../../vant-weapp/toast/toast';
import fw from '../../core/framework.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    paging: {
      index: 0,
      size: 6,
      totalPages: 0,
      totalElements: 0
    },
    _paging:{
      index: 0,
      size: 6,
      totalPages: 0,
      totalElements: 0
    },
    isLogin: false,
    data:[],
    keywords: '',
    searchTimer: null,
    searchStatus: 0,
    btnsShow: false,
    btnsActions: [
      {
        name: '编辑',
        value: 'edit'
      },
      {
        name: '删除',
        value: 'delete'
      }
    ],
    currData: null,
    showPopup:false,
    currType:'create',
    name:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this;
    this.setData({
      isLogin: fw.user.islogin(),
      paging: this.data._paging
    });
    if (this.data.isLogin) {
      self.getData();
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (!this.data.isLogin) return false;
    this.setData({
      ['paging.index']: 0
    });
    this.getData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _paging = this.data.paging;
    if (_paging.totalElements != 0 && ((_paging.index + 1) * _paging.size > _paging.totalElements)) {
      return false;
    }
    this.setData({
      ['paging.index']: (_paging.index + 1)
    });
    this.getData('search', true);
  },
  search: function (event) {
    let self = this;
    if (!this.data.isLogin) return false;
    clearTimeout(self.data.searchTimer);
    self.setData({
      searchTimer: (setTimeout(function () {
        self.setData({
          keywords: event.detail,
          paging: self.data._paging
        });
        self.getData((event.detail || "").length > 0 ? 'search' : null);
      }, 100))
    })
  },
  getData(searchStaus, isPaging){
    let self = this;
    let _data = self.data;
    let _paging = _data.paging;
    let ajaxOpts = {
      index: _paging.index,
      size: _paging.size,
      name: _data.keywords || ''
    };
    var setSearchStatus = function () {
      self.setData({
        searchStatus: searchStaus == 'search' ? 1 : 2
      });
    };
    fw.ajax('alarmContactGroup/search', ajaxOpts).then(res => {
      if (res.code == 0) {
        _paging.totalElements = res.data.totalElements;
        self.setData({
          data: isPaging ? [...self.data.data, ...res.data.content] : res.data.content,
          paging: _paging,
          searchStatus: 0
        });
        if (res.data.content.length == 0) {
          setSearchStatus();
        }
      } else {
        setSearchStatus();
      }
    });
  },
  openHandle(e){
    var data = fw.getTargetData(e, 'person');
    this.setData({
      btnsShow: true,
      currData: data
    })
  },
  goCreate(){
    this.setData({
      currType:'create',
      showPopup:true,
      name:''
    })
  },
  closeBtnsShow(e) {
    this.setData({
      btnsShow: false
    })
  },
  chooseBtns(e) {
    var self = this;
    var currData = self.data.currData;
    switch (e.detail.value) {
      case 'edit':
        this.setData({
          currType:'edit',
          showPopup:true,
          name:currData.name
        })
        break;
      case 'delete':
        Dialog.confirm({
          title: '提示',
          message: '确认删除报警联系人组'+currData.name+'?',
        }).then(() => {
            fw.ajax('alarmContactGroup/delete',{id:currData.id}).then(res=>{
              if(res.code == 0){
                self.closeBtnsShow();
                self.onShow();
              }else{
                Toast.fail(res.msg);
              }
            })
        }).catch(() => {
           self.closeBtnsShow();
        });
        break;
      default:
        break;
    }
  },
  closePopup(){
    this.setData({
      showPopup:false
    })
  },
  setValue(event) {
    var _value = event.detail;
    var propName = fw.getTargetData(event, 'name');
    this.setData({
      [propName]: _value
    });
  },
  save(){
    var self = this;
    if(!self.data.name){
      Toast.fail('报警联系人组名称不能为空')
      return false;
    }
    if(self.data.currType == 'create'){
        fw.ajax('alarmContactGroup/create',{name:self.data.name}).then(res=>{
            self.closePopup();
            self.onShow();
        })
    }else{
      fw.ajax('alarmContactGroup/update',{id:self.data.currData.id,name:self.data.name}).then(res=>{
        self.closePopup();
        self.onShow();
    })
    }
  }
})